<?php

namespace Wulian\Controller;

use Common\Controller\AdminbaseController;

class AdminFlowController extends AdminbaseController {

    protected $m_card,$m_package,$m_order,$m_operate_log,$m_flow_recharge;
    protected $quota_type_list = array(
      1=>"流量",
        2=>"时长",
        3=>"条数",
    );

    function _initialize() {
        parent::_initialize();
        $this->m_card= D("Wulian/WlCard");
        $this->m_package = M("WlPackage");
        $this->m_order = M("WlOrder");
        $this->m_operate_log = D("WlOperateLog");
        $this->m_flow_recharge = D("WlFlowRecharge");
    }



    /**
     * 流量池
     */
    public function index(){
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        $flow = $telecom->getPoolList();
        $this->assign("pool",$flow['pool']);
        $this->display();
    }

    /**
     * 流量池月使用量查询
     */
    public function month(){
        $list = array();
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];

        $s_date = I("s_date");
        $_GET['s_date'] = $s_date;
        if(!empty($s_date)){
            $checkCard = "";
            $s_date = $s_date==2?0:$s_date;
            $this->assign("s_date",I("s_date"));

            $s_number = I("s_number");
            $this->assign("s_number",$s_number);
            $_GET['s_number'] = $s_number;
            $where = array();
            if(!empty($s_number)) {
                $where = array("card_iccid"=>$s_number);
                $checkCard = checkCardNumberReturnTxt($s_number);
            }

            $count=$this->m_card->where($where)->count();
            $page = $this->page($count, 10);
            $card = $this->m_card->where($where)->limit($page->firstRow , $page->listRows)->select();
            foreach($card AS $citem){
                $card_use = $telecom->getPoolMemQry($s_date,$citem['card_number']);
                if(isset($card_use['PoolInfo'])){
                    $card_use['PoolInfo']['pool_left'] = $card_use['pool_left'];
                    $card_use["PoolInfo"]["card_iccid"] = $citem["card_iccid"];
                    $list[] = $card_use['PoolInfo'];
                }
            }
            $pageHtml =  $page->show('Admin');
            $pageHtml = getAjaxPageHtml($pageHtml);


            foreach($list AS &$litem){
                if($_GET['s_date'] == 2){   //上月
                    $all_flow = getPreFlowByCardNumber($litem['ACC_NBR']);
                }else{
                    $all_flow = getFlowByCardNumber($litem['ACC_NBR']);
                }

                $litem["POOL_MEMBER_TOTAL"] = ($all_flow/$this->c_proportion)."M";
                $litem['POOL_MEMBER_ALREADY'] = kbToM($litem['POOL_MEMBER_ALREADY']);

                if($this->c_proportion == 1){   //代理商版本
                    $litem["POOL_MEMBER_ALREADY"] =  $litem["POOL_MEMBER_ALREADY"]*$this->wl_proportion;
                }
                $litem['POOL_MEMBER_ALREADY'] = $litem['POOL_MEMBER_ALREADY']."M";

                $litem['pool_left'] = kbToM($litem['pool_left'])."M";
            }
            $this->assign("list",$list);
            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "ACC_NBR"=>"成员号码",
                "POOL_MEMBER_TOTAL"=>"成员分配额度",
                "POOL_MEMBER_ALREADY"=>"成员支出量值",
                "pool_left"=>"池剩余量",
            );

            $export_list = array(
                "excel_title"=>"流量池成员使用量",
                "export_title"=>$export_title,
                "export_data"=>$list,
            );
            write_file("flow_month",$export_list);
            successJson($list,$pageHtml,$checkCard);
        }

        $pool = $telecom->getPoolQry();
        $pool['pool_total'] = kbToM($pool['pool_total']);
        $pool['pool_already'] = kbToM($pool['pool_already']);
        $pool['pool_left'] = kbToM($pool['pool_left']);
        $this->assign("pool",$pool);
        $this->display();
    }

    /**
     * 卡成员查询
     */
    public function member(){
        $list = array();
        if(isset($_POST["s_number"]) || isset($_GET["s_number"])) {
            $checkCard = "";
            $telecom = new \Wulian\Lib\telecom();
            $telecom->poolNbr = $this->wl_config['flow_pool_id'];

            $s_number = I("s_number");
            $_GET['s_number'] = $s_number;
            $where = array();
            if (!empty($s_number)) {
                $where = array("card_iccid"=>$s_number);
                $checkCard = checkCardNumberReturnTxt($s_number);
            }
            $count = $this->m_card->where($where)->count();
            $page = $this->page($count, 10);
            $card = $this->m_card->where($where)->limit($page->firstRow, $page->listRows)->select();
            foreach ($card AS $citem) {
                $member = $telecom->getPoolMember($citem['card_number']);
                if (isset($member['OutProdInfos'])) {
                    $member["OutProdInfos"]["card_iccid"] = $citem["card_iccid"];
                    $list[] = $member['OutProdInfos'];
                }
            }
            $pageHtml =  $page->show('Admin');
            $pageHtml = getAjaxPageHtml($pageHtml);

            foreach ($list AS &$item) {
                $item["quota_type"] = $this->quota_type_list[$item['quota_type']];
                $card = $this->m_card->where(array("card_number"=>$item["acc_nbr"]))->find();
                $item["package_data_month"] = ($card["package_data_month"]/$this->c_proportion)."M";
                $item["package_data_add"] = ($card["package_data_add"]/$this->c_proportion)."M";
                $item["flow_quota"] = $item["package_data_month"];
                if(!empty($card["package_data_add"])){
                    $item["flow_quota"] = $item["package_data_month"]."+".$item["package_data_add"];
                }

            }
            //设置导出数据
            $export_title = array(
                "card_iccid" => "成员号码",
                "acc_nbr" => "成员号码",
                "flow_quota" => "流量限额",
                "quota_type" => "限额类型",
                "org_id" => "归属地",
                "eff_date" => "生效时间",
                "state" => "状态",
            );

            $export_list = array(
                "excel_title" => "流量池成员信息",
                "export_title" => $export_title,
                "export_data" => $list,
            );
            write_file("flow_member", $export_list);

            successJson($list,$pageHtml,$checkCard);
        }
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 新增
     */
    public function add_member(){

        $this->display();
    }

    /**
     * 成员流量修改
     */
    public function modify_member(){

        $card_number = I("card_number");
        if(empty($card_number)){
            $this->error("参数为空!");exit;
        }
        $package_list = $this->m_package->where(array("package_type"=>1,"is_del"=>0))->select();
        $this->assign("package_list",$package_list);
        $card = $this->m_card->where(array("card_number"=>$card_number))->find();

        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        $member = $telecom->getPoolMember($card_number);
        $item = array();
        if(isset($member['OutProdInfos'])){
            $item = $member['OutProdInfos'];
            $item["flow_quota"] =  $card["package_data_month"];
        }

        $this->assign("item",$item);
        $this->display();
    }

    /**
     * 修改提交
     */
    public function modify_member_post(){
        $member_number  = I("member_number");
        $package_id = I("package_id");
        if(empty($member_number) || empty($package_id)){
            $this->error("参数为空!");exit;
        }
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        $package = $this->m_package->where(array("package_id"=>$package_id))->find();
        $card = $this->m_card->where(array("card_number"=>$member_number))->find();
        if(empty($card)){
            $this->error("该会员卡不存在!");exit;
        }
        $result = $telecom->modifyPoolMember($member_number,(int)(($package["package_data"]+$card["package_data_add"])/$this->wl_proportion));
        if($result["Response"]["RspCode"] == 0000){
            $result = addOrder($this->wl_config['flow_pool_id'],$member_number,$package_id,1);
            addLog(3,"流量池成员 修改月套餐");
            $this->success("修改成功!");exit;

        }else{
            $this->error("修改失败!");exit;
        }
    }

    /**
     * 批量导入
     */
    public function batch_modify_member(){
        $package_list = $this->m_package->where(array("package_type"=>1,"is_del"=>0))->select();
        $this->assign("package_list",$package_list);
        $this->display();
    }

    /**
     * 批量修改提交
     */
    public function batch_modify_member_post()
    {
        $package_id = I("package_id");
        if(empty($package_id)){
            $this->error("请选择月套餐!");exit;
        }
        $file = $_FILES['card_file']['tmp_name'];
        $file_type = $_FILES['card_file']['type'];

        if (!in_array($file_type, getExcelType())) {
            $this->error("文件格式有误！");
            exit;
        }
        if (empty($file)) {
            $this->error("文件不存在！");
            exit;
        }
        $excel = new \Common\Lib\excel($file);
        $excel->column_list = array("card_iccid");
        $card_list = $excel->get_excel_data($file_type);
        if (empty($card_list)) {
            $this->error("文件数据为空！");
            exit;
        }
        $error_list = array();
        $package = $this->m_package->where(array("package_id"=>$package_id))->find();
        if(empty($package)){
            $this->error("您选择的套餐不存在！");
            exit;
        }
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        foreach ($card_list AS $cindex => $citem) {
            $card = $this->m_card->where(array("card_iccid"=>$citem["card_iccid"]))->find();
            if(!empty($card)){
                $result = $telecom->modifyPoolMember($card["card_number"],(int)($package["package_data"]+$card["package_data_add"])/$this->wl_proportion);
                if($result["Response"]["RspCode"] == '0000'){
                    $result = addOrder($this->wl_config['flow_pool_id'],$card["card_number"],$package_id,1);
                }else{
                    $error_list[] = "第".($cindex+2)."行:<".$citem["card_iccid"].">修改失败";
                }
            }else{
                $error_list[] = "第".($cindex+2)."行:<".$citem["card_iccid"].">尚未导入";
            }

        }
        addLog(3,"流量池成员 批量修改月套餐");

        $this->assign("error_list", $error_list);
        $this->display("AdminCard/import_success");
    }

    /**
     * 充值
     */
    public function recharge(){
        $this->assign("flow_pool_id",$this->wl_config["flow_pool_id"]);
        $this->display();
    }

    /**
     * 流量池充值记录
     */
    public function recharge_log(){
        $s_start_time = I("s_start_time");
        $s_end_time = i("s_end_time");

        if(empty($s_start_time)){
            $s_start_time = date("Y-m-d",time()-(86400*7));
        }
        if(empty($s_end_time)){
            $s_end_time = date("Y-m-d");
        }
        $where = array(
            "create_time"=>array("between",array($s_start_time." 00:00:00",$s_end_time." 23:59:59"))
        );

        $count=$this->m_flow_recharge->where($where)->count();
        $page = $this->page($count, 15);
        $list = $this->m_flow_recharge->where($where)
            ->order("create_time DESC")
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("s_end_time",$s_end_time);
        $this->assign("s_start_time",$s_start_time);
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 充值提交
     */
    public function recharge_post(){
        $flow_number = I("post.flow_number");   //充值量
        if(!empty($flow_number)){
            if(!((int)$flow_number == (float)$flow_number && $flow_number>0)){
                $this->error("充值量必须为正整数!",U('AdminFlow/recharge'));exit;
            }

            $telecom = new \Wulian\Lib\telecom();
            $telecom->poolNbr = $this->wl_config['flow_pool_id'];
            $result = $telecom->flowPool((int)$flow_number);
            if($result["IRESULT"] == '0'){
                addLog(5,"流量池 充值");
                $data = array(
                    "flow_pool_id"=> $this->wl_config['flow_pool_id'],
                    "flow_recharge_plus"=>0,
                    "flow_recharge_total"=>0,
                    "flow_recharge_already"=>0,
                    "flow_recharge_num"=>$flow_number,
                );
                addFlowRechargeLog($data);


                $sms = new \Wulian\Lib\sms();
                $sms->sendFlowRecharge(array("name"=>session("nickname"),"datetime"=>date("Y-m-d H:i:s"),"num"=>$flow_number."G"));

                $this->success("充值成功！");
                exit;
            }else{
                $this->error("充值失败！");
                exit;
            }

        }
    }
}
